package com.example.forumsystem.demos.web.model;

import com.example.forumsystem.demos.web.common.ResultCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class Result<T> {
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;  //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String errMsg;  //当出现自定义异常时的描述信息

    public Result(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.data = data;
        this.message = ResultCode.SUCCESS.getMessage();
    }

    public Result(String message) {
        this.code = ResultCode.SUCCESS.getCode();
        this.message = message;
        this.data = null;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(int code, String message, String errMsg) {
        this.code = code;
        this.message = message;
        this.errMsg = errMsg;
    }

    public Result(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static<T> Result <T> success(T data){
        return new Result<>(data);
    }

    public static Result success(){
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }
    public static<T> Result <T> success(ResultCode resultCode){
        return new Result<>(resultCode.getCode(), resultCode.getMessage());
    }
    public static Result success(String message){
        return new Result(ResultCode.SUCCESS.getCode(), message, "");
    }

    public static<T> Result <T> success(String message, T data){
        return new Result<>(ResultCode.SUCCESS.getCode(), message, data);
    }


    public static<T> Result <T> fail(String message){
        return new Result<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), message);
    }

    public static Result fail(){
        return new Result(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static Result fail(ResultCode resultCode){
        return new Result(resultCode.getCode(), resultCode.getMessage());
    }
}
